﻿using System;
using System.Linq;
using System.Web;
using ASPNETVN.PORTAL.Modules.ACTP.Components;

namespace ASPNETVN.PORTAL.Modules.ACTP.Controls
{
    public partial class ProjectDetailControl : System.Web.UI.UserControl
    {
        private int _ProjectID;
        public int ProjectID
        {
            get
            {
                if (_ProjectID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["ProjectID"] != null)
                        _ProjectID = Int32.Parse(Request.Params["ProjectID"]);
                }
                return _ProjectID;
            }
            set { _ProjectID = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                // BEGIN: Project details

                if (this.ProjectID > 0)
                {
                    Project project = Project.Load(this.ProjectID);
                    if (project != null)
                    {
                        Team team = Team.Load(project.TeamID);
                        LabelTeam.Text = team.Title;

                        ProjectEmployee pe = ProjectEmployee.SelectCollectionBy_ProjectID(project.ProjectID).FirstOrDefault(x =>x.IsProjectManager);
                        if (pe != null)
                        {
                            Employee projectManager = Employee.Load(pe.EmployeeID);
                            if (projectManager != null)
                            {
                                LabelProjectManager.Text = string.Format("(CODE: {0}) - {1} {2} {3}", projectManager.EmployeeCode, projectManager.PersonalTitle, projectManager.FirstName, projectManager.LastName);
                            }
                        }
                        
                        LabelCode.Text = project.Code;
                        LabelSubject.Text = project.Subject;
                        if (project.StartDate.Year > 1753)
                        {
                            LabelStartDate.Text = project.StartDate.ToShortDateString();
                        }

                        if (project.DueDate.Year > 1753)
                        {
                            LabelDueDate.Text = project.DueDate.ToShortDateString();
                        }

                        if (project.CompletedDate.Year > 1753)
                        {
                            LabelCompletedDate.Text = project.CompletedDate.ToShortDateString();
                        }

                        LabelPriority.Text = Project.GetPriorityName(project.Priority);
                        LabelStatus.Text = project.Status;
                        LabelCompletedPercent.Text = project.CompletedPercent.ToString();
                    }
                }
                // END: Project detail
            }
        }
    }
}